﻿using System;
using System.Linq;
using System.ComponentModel;
using System.Windows.Data;
using System.Collections.ObjectModel;

using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;

using StudentModule.Models;
using StudentModule.Services;
using ServiceAgent;
using ServiceAgent.CatalogueService;
using Infrastructure;
using Infrastructure.Helpers;
using Infrastructure.Events;
using System.Windows.Input;
using System.Windows;
using Microsoft.Practices.Unity;
using Microsoft.Practices.ServiceLocation;
using StudentModule.Events;
using Infrastructure.InteractionRequests;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using ServiceAgent.StudentService;
using Infrastructure.Account;

namespace StudentModule.ViewModels
{
    /// <summary>
    /// ViewModel for HoSoDaNopView.
    /// </summary>
    public class HoSoDaNopViewModel : NotificationObject, INavigationAware, IPopupWindowActionAware
    {
        #region Fields
        IUnityContainer container;
        IEventAggregator eventAggregator;

        int studentID;
        ObservableCollection<HoSoDaNopDTO> hoSoList;

        #endregion

        #region Constructor
        /// <summary>
        /// Default constructor.
        /// </summary>
        public HoSoDaNopViewModel()
        {
            this.container = ServiceLocator.Current.GetInstance<UnityContainer>();
            this.eventAggregator = ServiceLocator.Current.GetInstance<EventAggregator>();
            this.eventAggregator.GetEvent<NopHoSoEvent>().Subscribe(GetStudent);
            this.eventAggregator.GetEvent<SendHoSoDaNopEvent>().Subscribe(GetList);
            this.Initialize();
        }
        #endregion

        #region Administrative Properties
        // IPopupWindowActionAware implementation
        public Window HostWindow { get; set; }

        public Notification HostNotification { get; set; }

        public int StudentID
        {
            get { return studentID; }
            set { studentID = value; }
        }

        public ObservableCollection<HoSoDaNopDTO> HoSoList
        {
            get { return hoSoList; }
            set
            {
                hoSoList = value;
                RaisePropertyChanged(() => this.HoSoList);
            }
        }
        #endregion

        #region Command Properties
        public ICommand SaveCommand { get; private set; }
        public ICommand CancelCommand { get; set; }
        #endregion

        #region Private Methods
        private void Initialize()
        {
            this.SaveCommand = new DelegateCommand(this.ExecuteSave);
            this.CancelCommand = new DelegateCommand(this.ExecuteCancel);
        }

        void ExecuteSave()
        {
            try
            {
                ObservableCollection<HoSoDaNopDTO> setList = new ObservableCollection<HoSoDaNopDTO>();
                ObservableCollection<HoSoDaNopDTO> unsetList = new ObservableCollection<HoSoDaNopDTO>();

                foreach (var hoso in HoSoList)
                {
                    if (hoso.IsTrue && hoso.ID == 0)
                    {
                        hoso.NhanVienNhan = ActiveAccount.Instance.TenTaiKhoan;
                        setList.Add(hoso);
                    }
                    else
                    {
                        if (!hoso.IsTrue && hoso.ID != 0)
                        {
                            unsetList.Add(hoso);
                        }
                    }
                }
                if (setList.Count() > 0)
                {
                    using (var proxy = container.Resolve<StudentServiceAgent>())
                    {
                        proxy.SetHoSoDaNopListForSinhVien(StudentID, setList);
                    }
                }

                if (unsetList.Count() > 0)
                {
                    using (var proxy = container.Resolve<StudentServiceAgent>())
                    {
                        proxy.UnsetHoSoDaNopListForSinhVien(StudentID, unsetList);
                    }
                }
                if (this.HostWindow != null)
                {
                    this.HostWindow.Close();
                }
            }
            catch (System.Exception)
            {
                MessageBox.Show("Có lỗi");
            }

        }

        void ExecuteCancel()
        {
            if (this.HostWindow != null)
            {
                this.HostWindow.Close();
            }
        }

        void GetStudent(int ID)
        {
            this.StudentID = ID;
        }

        void GetList(ObservableCollection<HoSoDaNopDTO> list)
        {
            this.HoSoList = list;
        }
        #endregion

        #region INavigationAware Members
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            // Called to see if this view can handle the navigation request.
            // If it can, this view is activated.
            return false;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

            // Called when this view is deactivated as a result of navigation to another view.
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            //// Called to initialize this view during navigation.

            //// Retrieve any required paramaters from the navigation Uri.
            //string id = navigationContext.Parameters["ID"];
            //if (!string.IsNullOrEmpty(id))
            //{
            //    DataItem item = _dataService.GetModel().FirstOrDefault(dataItem => dataItem.Id == id);
            //}
        }
        #endregion
    }
}
